﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using Spaanjaars.ContactManager.Infrastructure.UnitOfWork;
using Spaanjaars.ContactManager.Model;
using Spaanjaars.ContactManager.Services.Commands;
using Spaanjaars.ContactManager.Services.Implementations;
using Spaanjaars.ContactManager.Services.Interfaces;

namespace Spaanjaars.ContactManager.Tests.Service
{
    [TestFixture]
    public class When_submitting_a_valid_command_to_create_a_contact_person
    {
        private IContactPersonRepository _contactPersonRepository;
        private IUnitOfWork _uow;
        private CreateContactPersonCommandResult _response;
        private MockRepository _mocks;
        private IContactPersonCreationService _contactPersonCreationService;
        private CreateContactPersonCommand _contactPerson;

        private string _firstName = "Scott";
        private string _middleName = string.Empty;
        private string _lastName = "Millett";
        private DateTime _dateOfBirth = DateTime.Now.AddYears(-10);

        [SetUp]
        public void Context()
        {
            _mocks = new MockRepository();

            _contactPersonRepository = _mocks.StrictMock<IContactPersonRepository>();
            _uow = _mocks.StrictMock<IUnitOfWork>();

            _contactPersonCreationService = new ContactPersonCreationService(_contactPersonRepository, _uow);

            _contactPerson = new CreateContactPersonCommand()
                                 {
                                     FirstName = _firstName,
                                     MiddleName = _middleName,
                                     LastName = _lastName,
                                     DateOfBirth = _dateOfBirth
                                 };
        }

        [Test]
        public void A_contact_person_entity_is_persisted_via_the_repository()
        {
            using (_mocks.Record())
            {
                Expect.Call(delegate { _contactPersonRepository.Add(null); }).IgnoreArguments();
                Expect.Call(_uow.Commit);
                
            }
            using (_mocks.Playback())
            {
                _response = _contactPersonCreationService.CreatePersonFrom(_contactPerson);
            }            
        }

        [Test]
        public void The_response_contain_no_broke_rules()
        {            

            _response = _contactPersonCreationService.CreatePersonFrom(_contactPerson);

            Assert.That(_response.BrokenRules.Count().Equals(0));
        }

        [Test]
        public void The_response_should_contain_a_view_model_of_the_contact_person()
        {

            _response = _contactPersonCreationService.CreatePersonFrom(_contactPerson);

            var contactPersonViewModel = _response.ContactPersonViewModel;

            Assert.IsNotNull(contactPersonViewModel);
            Assert.That(contactPersonViewModel.DateOfBirth.ToString().Equals(_dateOfBirth));
            Assert.That(contactPersonViewModel.Name.Equals(String.Format("{0} {1}", _firstName, _lastName)));            

        }
    }
}
